package ac.rsync.download;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

//这个是任务Bean

public class Task {
	private String downURL;
	private String saveFile;

	private int bufferSize = 64 * 1024;

	private int workerCount;

	private int sectionCount;

	private long contentLength;

	private long[] sectionsOffset;

	public static final int HEAD_SIZE = 4096;

	// 读下载描述文件内容

	public synchronized void read(RandomAccessFile file) throws IOException {

		byte[] temp = new byte[HEAD_SIZE];

		file.seek(0);

		int readed = file.read(temp);

		if (readed != temp.length) {

			throw new RuntimeException();

		}

		ByteArrayInputStream bais = new ByteArrayInputStream(temp);

		DataInputStream dis = new DataInputStream(bais);

		downURL = dis.readUTF();

		saveFile = dis.readUTF();

		sectionCount = dis.readInt();

		contentLength = dis.readLong();

		sectionsOffset = new long[sectionCount];

		for (int i = 0; i < sectionCount; i++) {

			sectionsOffset[i] = file.readLong();

		}

	}

	// 创建下载描述文件内容

	public synchronized void create(RandomAccessFile file) throws IOException {

		if (sectionCount != sectionsOffset.length) {

			throw new RuntimeException();

		}

		long len = HEAD_SIZE + 8 * sectionCount;

		file.setLength(len);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		DataOutputStream dos = new DataOutputStream(baos);

		dos.writeUTF(downURL);

		dos.writeUTF(saveFile);

		dos.writeInt(sectionCount);

		dos.writeLong(contentLength);

		byte[] src = baos.toByteArray();

		byte[] temp = new byte[HEAD_SIZE];

		System.arraycopy(src, 0, temp, 0, src.length);

		file.seek(0);

		file.write(temp);

		writeOffset(file);

	}

	// 更新下载的过程

	public synchronized void writeOffset(RandomAccessFile file)
			throws IOException {

		if (sectionCount != sectionsOffset.length) {

			throw new RuntimeException();

		}

		file.seek(HEAD_SIZE);

		for (int i = 0; i < sectionsOffset.length; i++) {

			file.writeLong(sectionsOffset[i]);

		}

	}
	
	public void setDownURL(String url)
	{
		this.downURL = url;
	}
	
	public void setSaveFile(String fs)
	{
		this.saveFile = fs;
	}
	
	public void setSectionCount(int sc)
	{
		this.sectionCount = sc;
	}
	
	public void setWorkerCount(int wc)
	{
		this.workerCount = wc;
	}
	
	public void setContentLength(long cL)
	{
		this.contentLength = cL; 
	}
	
	public Long getContentLength()
	{
		return this.contentLength;
	}
	
	public void setBufferSize(int bs)
	{
		this.bufferSize = bs;
	}
	
	public String getSaveFile()
	{
		return this.saveFile;
	}
	
	public String getDownURL()
	{
		return this.downURL;
	}

	public int getSectionCount()
	{
		return this.sectionCount;
	}
	
	public int getWorkerCount()
	{
		return this.workerCount;
	}
	
	public void setSectionsOffset(long[] so)
	{
		this.sectionsOffset = so;
	}
	
	public long[] getSectionsOffset()
	{
		return this.sectionsOffset;
	}
	
	public Integer getBufferSize()
	{
		return this.bufferSize;
	}
}
